<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=QDvLUEl5NjFnbTDP9fu19VwghS9mNTPS"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }

        #allmap,
        #allmap_{
            height: 450px;
            width: 100%;
            overflow: hidden;
            font-family: "微软雅黑";
            border: 1px solid green;
        }

        /*百度地图搜索框定位*/
        .tangram-suggestion-main {
            z-index: 999999;
        }

        /*设置弹出框样式*/
        .el-dialog__body {
            padding: 0px 20px;
            color: #606266;
            font-size: 14px;
        }

        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

    </style>
    <style type="text/css">
        html {
            height: 100%
        }

        body {
            height: 100%;
            margin: 0px;
            padding: 0px
        }

        #container {
            height: 100%
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>首页
            <small>公司分布图</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>公司分布图</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <template>
            <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-tab-pane label="分布图" name="first">
                    <iframe name="right" class="el-main" src="map.html" width="100%" height="580px"
                            frameborder="0"></iframe>
                </el-tab-pane>

                <el-tab-pane label="详细信息" name="second">
                    <div class="filter-container">
                        <el-input placeholder="公司地址" v-model="pagination.queryString" style="width: 200px;"
                                  class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="organizationName" label="体检机构名称" align="center"></el-table-column>
                        <el-table-column prop="telephone" label="联系电话" align="center"></el-table-column>
                        <el-table-column prop="lng" label="所在经度" align="center"></el-table-column>
                        <el-table-column prop="lat" label="所在纬度" align="center"></el-table-column>
                        <el-table-column prop="addressDetail" label="详细地址" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                                class="pagination"
                                @current-change="handleCurrentChange"
                                :current-page="pagination.currentPage"
                                :page-size="pagination.pageSize"
                                layout="total, prev, pager, next, jumper"
                                :total="pagination.total">
                        </el-pagination>
                    </div>

                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增机构地址" :visible.sync="dialogFormVisible" @opened="createMap">
                            <el-form ref="dataAddForm" :model="formData" label-position="left"
                                     label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="机构名称" prop="organizationName">
                                            <el-input v-model="formData.organizationName"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="&emsp;&emsp;联系电话" prop="telephone">
                                            <el-input v-model="formData.telephone"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="上传体检机构地址图片">
                                            <el-upload
                                                    class="avatar-uploader"
                                                    action="/address/upload"
                                                    :auto-upload="autoUpload"
                                                    name="imgFile"
                                                    :show-file-list="false"
                                                    :on-success="handleAvatarSuccess"
                                                    :before-upload="beforeAvatarUpload">
                                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                            </el-upload>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="详细地址" prop="addressDetail">
                                            <el-input id="suggestId" v-model="formData.addressDetail"
                                                      style="width: 400px;"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="经度" prop="lng">
                                            <el-input v-model="userlocation.lng"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="&emsp;&emsp;纬度" prop="lat">
                                            <el-input v-model="userlocation.lat"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <div id="allmap"></div>
                                <div id="searchResultPanel"
                                     style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>


                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑机构地址" :visible.sync="dialogFormVisible4Edit" @opened="showMap">
                            <el-form ref="dataAddForm_" :model="formData" label-position="left"
                                     label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="机构名称" prop="organizationName">
                                            <el-input v-model="formData.organizationName"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="&emsp;&emsp;联系电话" prop="telephone">
                                            <el-input v-model="formData.telephone"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="上传体检机构地址图片">
                                            <el-upload
                                                    class="avatar-uploader"
                                                    action="/address/upload"
                                                    :auto-upload="autoUpload"
                                                    name="imgFile"
                                                    :show-file-list="false"
                                                    :on-success="handleAvatarSuccess"
                                                    :before-upload="beforeAvatarUpload">
                                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                            </el-upload>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="详细地址" prop="addressDetail">
                                            <el-input id="suggestId_" v-model="formData.addressDetail"
                                                      style="width: 400px;"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="经度" prop="lng">
                                            <el-input v-model="userlocation.lng"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="&emsp;&emsp;纬度" prop="lat">
                                            <el-input v-model="userlocation.lat"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <div id="allmap_"></div>
                                <div id="searchResultPanel_"
                                     style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handlerEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                </el-tab-pane>
            </el-tabs>
        </template>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: "second",
            autoUpload: true,//自动上传
            imageUrl: null,//模型数据，用于上传图片完成后图片预览
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 5,
                total: 0,
                queryString: null,
            },
            formData: {
                lng: '',
                lat: ''
            },//表单数据
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
            userlocation: {lng: "", lat: ""}
        },
        created() {
            this.findPage();
        },
        methods: {
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },

            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) {
                //提示上传结果
                this.$message({
                    type: response.flag ? "success" : "error",
                    message: response.message
                });
                //设置图片地址
                this.imageUrl = "http://picture.eznotez.com/" + response.data;
                this.formData.img = response.data;
            },

            createMap() {
                const _this = this;

                function G(id) {
                    return document.getElementById(id);
                }

                var map = new BMap.Map("allmap");
                map.centerAndZoom("杭州", 12);        // 初始化地图,设置城市和地图级别。

                // map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
                var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                    {
                        "input": "suggestId",
                        "location": map
                    });

                ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
                    var str = "";
                    var _value = e.fromitem.value;
                    var value = "";
                    if (e.fromitem.index > -1) {
                        value = _value.province + _value.city + _value.district + _value.street + _value.business;
                    }
                    str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                    value = "";
                    if (e.toitem.index > -1) {
                        _value = e.toitem.value;
                        value = _value.province + _value.city + _value.district + _value.street + _value.business;
                    }
                    str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                    G("searchResultPanel").innerHTML = str;
                });

                var myValue;
                ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
                    var _value = e.item.value;
                    myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
                    G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

                    _this.formData.addressDetail = myValue;
                    setPlace();

                });

                function setPlace() {
                    map.clearOverlays();    //清除地图上所有覆盖物
                    function myFun() {
                        let pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                        _this.userlocation = pp;

                        map.centerAndZoom(pp, 18);
                        map.addOverlay(new BMap.Marker(pp));    //添加标注

                    }

                    var local = new BMap.LocalSearch(map, { //智能搜索
                        onSearchComplete: myFun
                    });

                    local.search(myValue);
                }
            },
            //分页查询
            findPage() {
                axios.post("/address/findPage", this.pagination).then((res) => {
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                })
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.imageUrl = null;
            },
            // 弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.dialogFormVisible4Edit = true;
                axios.get("/address/findById/" + row.id).then(response => {

                    if (response.data.flag) {
                        console.log(response.data.data);
                        this.formData = response.data.data;
                        this.userlocation.lng = this.formData.lng;
                        this.userlocation.lat = this.formData.lat;
                    }
                    if (this.formData.img != null) {
                        this.imageUrl = "http://picture.eznotez.com/" + this.formData.img;
                    } else {
                        this.imageUrl = null;
                    }

                })
            },
            showMap(){
                const _this = this;

                function G(id) {
                    return document.getElementById(id);
                }

                var map_ = new BMap.Map("allmap_");
                map_.centerAndZoom("杭州", 12);        // 初始化地图,设置城市和地图级别。

                // map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
                var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                    {
                        "input": "suggestId_",
                        "location": map_
                    });

                ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
                    var str = "";
                    var _value = e.fromitem.value;
                    var value = "";
                    if (e.fromitem.index > -1) {
                        value = _value.province + _value.city + _value.district + _value.street + _value.business;
                    }
                    str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                    value = "";
                    if (e.toitem.index > -1) {
                        _value = e.toitem.value;
                        value = _value.province + _value.city + _value.district + _value.street + _value.business;
                    }
                    str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                    G("searchResultPanel_").innerHTML = str;
                });

                var myValue;
                ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
                    var _value = e.item.value;
                    myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
                    G("searchResultPanel_").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

                    _this.formData.addressDetail = myValue;
                    setPlace();

                });

                function setPlace() {
                    map_.clearOverlays();    //清除地图上所有覆盖物
                    function myFun() {
                        let pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                        _this.userlocation = pp;

                        map_.centerAndZoom(pp, 18);
                        map_.addOverlay(new BMap.Marker(pp));    //添加标注

                    }

                    var local = new BMap.LocalSearch(map_, { //智能搜索
                        onSearchComplete: myFun
                    });

                    local.search(myValue);
                }
            },

            //编辑
            handlerEdit() {
                this.dialogFormVisibleEdit = false;
                ////发送异步请求
                axios.put("/address/edit", this.formData).then(response => {
                    if (response.data.flag) {
                        this.$message.success(response.data.message);
                        // this.dialogFormVisible4Edit = false
                        location.reload();
                        return;
                    }
                    this.$message.error(response.data.message);
                }).finally(() => this.findPage());
            },

            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm("您确定要删除此记录吗?", "提示", {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送异步请求删除体检套餐数据
                    axios.delete("/address/delete/" + row.id).then(response => {
                        if (response.data.flag) {
                            this.$message.success(response.data.message);
                            this.findPage();
                            return
                        }
                        this.$message.error(response.data.message);
                    }).finally(() => this.findPage());
                });
            },
            handleAdd() {
                console.log(this.userlocation);
                console.log(this.formData);
                //发送异步请求 ,完成数据添加
                this.formData.lng = this.userlocation.lng;
                this.formData.lat = this.userlocation.lat;
                this.dialogFormVisible = false;
                axios.post("/address/add", this.formData).then(response => {
                    //添加成功
                    if (response.data.flag) {
                        //展示提示信息
                        this.$message.success(response.data.message);
                        return;
                    }
                    //添加失败
                    this.$message.error(response.data.message);

                }).finally(() => this.findPage())

            },
            handleClick(tab, event) {

            }
        }
    });
</script>

</html>